package es.us.idinfor.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import es.us.idinfor.exception.IllegalSituation;

public class InputFlow implements Iterable<String> {
	private String filename;

	public InputFlow(String file) {
		filename = file;
	}

	public Iterator<String> iterator() {
		return new InputFlowIterator();
	}

	private class InputFlowIterator implements Iterator<String> {
		private BufferedReader bf;
		private String line;

		public InputFlowIterator() {
			try {
				bf = new BufferedReader(new FileReader(filename));
				line = bf.readLine();
			} catch(IOException e) {
				throw new IllegalSituation("It can't access to the input file.");
			}
		}

		public boolean hasNext() {
			return line != null;
		}

		public String next() {
			String pal = line;
			
			try {
				line = bf.readLine();
			} catch(IOException e) {
				throw new IllegalSituation("It can't access to the input file.");
			}
			
			return pal;
		}

		public void remove() {
			throw new UnsupportedOperationException();
		}
	}
}
